<template>
  <el-dialog
    title="新增学员"
    :visible.sync="visible"
    width="600px"
    :close-on-click-modal="false"
    class="dialog-container"
    @close="handlerClose"
  >
    <el-form ref="studentForm" :model="formData" :rules="rules" size="small" label-width="92px">
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="formData.phone" />
      </el-form-item>
      <el-form-item label="考试时间" prop="time">
        <el-date-picker
          v-model="formData.time"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        />
        <div class="edu-tips">为空表示随时可以作答</div>
      </el-form-item>
      <el-form-item label="作答次数" prop="exam_number">
        <el-input-number v-model="formData.exam_number" :min="0" :step="1" />
        <div class="edu-tips">0 表示不限制作答次数</div>
      </el-form-item>
      <el-form-item label="及格线" prop="exam_score_pass">
        <el-input-number v-model="formData.exam_score_pass" :min="0" :step="1" :max="paper.total_score" :precision="1" /> <span>（试卷总分：{{ paper.total_score }}）</span>
      </el-form-item>
      <el-form-item label="人脸验证" prop="face_verification">
        <el-switch v-model="formData.face_verification" :active-value="1" :inactive-value="0"></el-switch>
        <div class="edu-tips">开启后，进入考试需要进行人脸验证</div>
      </el-form-item>
    </el-form>
    <form-footer slot="footer" :loading="confirmLoading" class="dialog-footer" @cancel="handlerClose" @submit="handlerConfirm" />
  </el-dialog>
</template>

<script>
import FormFooter from '@/components/form-footer'
export default {
  name: 'CreatedDialog',
  components: {
    FormFooter
  },
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    paperId: {
      type: [Number, String],
      required: true
    }
  },
  data() {
    return {
      confirmLoading: false,
      formData: {
        phone: undefined,
        time: '',
        exam_number: 0,
        face_verification: 0,
        exam_score_pass: 0
      },
      paper: {
        total_score: 0
      },
      rules: {
        phone: [{ required: true, message: '请输入学员手机号', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.loadBaseInfo()
  },
  methods: {
    loadBaseInfo() {
      this.$http.get(
          '/exam/admin/paper/' + this.paperId + '/base/info',
          {},
          ({ data }) => {
            this.paper = data
          },
          ({ msg }) => {
            this.$message.error(msg)
            this.confirmLoading = false
          }
      )
    },
    // 关闭后重置字段
    handlerClose() {
      this.$emit('update:visible', false);
    },
    // 新增缺员
    handlerConfirm() {
      this.$refs.studentForm.validate((valid) => {
        if (valid) {
          this.confirmLoading = true
          this.$http.post(
              '/exam/admin/paper/' + this.$route.params.id + '/student/create',
              this.formData,
              ({ msg }) => {
                this.$message.success(msg)
                this.confirmLoading = false
                this.handlerClose()
                this.$emit('reload');
              },
              ({ msg }) => {
                this.$message.error(msg)
                this.confirmLoading = false
              }
          )
        }
      })
    }
  }
}
</script>

<style scoped lang="scss">
.dialog-container{
  .type-list{
    .type-list-item{
      margin-bottom: 10px;
      &:last-child{
        margin-bottom: 0;
      }
      .el-select{
        width: 100%;
      }
    }
  }
}
</style>
